package GUILayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ControlLayer.*;
import ModelLayer.*;

public class NewCustomer extends JFrame {

	private JPanel contentPane;
	
	private IFSearch search;
	private IFCreate create;
	
	private JPanel panelPrivate;
	private JPanel panelCompany;
	
	private int nextContactID;
	private String nextID;
	
	private boolean errorP;
	private boolean errorZ;

	/**
	 * Create the frame.
	 */
	public NewCustomer() {
		
		create = new CreateControler();
		
		nextContactID = create.getNextContactId();
		nextID = Integer.toString(nextContactID);
		
		setResizable(false);
		setTitle("New Customer");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(150, 50, 300, 360);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		JButton btnPrivateCustomer = new JButton("Private Customer");
		btnPrivateCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelPrivate.setVisible(true);
				panelCompany.setVisible(false);
			}
		});
		btnPrivateCustomer.setBounds(10, 11, 126, 23);
		contentPane.add(btnPrivateCustomer);
	
		JButton btnCompanyCustomer= new JButton("Company customer");
		btnCompanyCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelPrivate.setVisible(false);
				panelCompany.setVisible(true);
			}
		});
		btnCompanyCustomer.setBounds(146, 11, 126, 23);
		contentPane.add(btnCompanyCustomer);

		
		drawPrivate();
		drawCompany();
		
	}
	private void drawPrivate()
	{
		panelPrivate = new JPanel();
		panelPrivate.setBounds(10, 45, 278, 275);
		panelPrivate.setBackground(new Color(255, 255, 204));
		contentPane.add(panelPrivate);
		panelPrivate.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 89, 14);
		panelPrivate.add(lblId);
		
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(10, 36, 89, 14);
		panelPrivate.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(10, 61, 89, 14);
		panelPrivate.add(lblLastName);
		
		JLabel lblCpr = new JLabel("CPR:");
		lblCpr.setBounds(10, 86, 89, 14);
		panelPrivate.add(lblCpr);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 111, 89, 14);
		panelPrivate.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(10, 136, 89, 14);
		panelPrivate.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(10, 161, 89, 14);
		panelPrivate.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 186, 89, 14);
		panelPrivate.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 211, 89, 14);
		panelPrivate.add(lblEmail);
		
		final JTextField txtFName = new JTextField();
		txtFName.setBounds(121, 33, 118, 20);
		panelPrivate.add(txtFName);
		txtFName.setColumns(10);
		
		final JTextField txtLName = new JTextField();
		txtLName.setBounds(121, 58, 118, 20);
		panelPrivate.add(txtLName);
		txtLName.setColumns(10);
		
		final JTextField txtCPR = new JTextField();
		txtCPR.setBounds(121, 83, 118, 20);
		panelPrivate.add(txtCPR);
		txtCPR.setColumns(10);
		
		final JTextField txtAddress = new JTextField();
		txtAddress.setBounds(121, 108, 118, 20);
		panelPrivate.add(txtAddress);
		txtAddress.setColumns(10);
		
		final JTextField 	txtCity = new JTextField();
		txtCity.setBounds(121, 133, 118, 20);
		panelPrivate.add(txtCity);
		txtCity.setColumns(10);
		
		final JTextField 	txtZIP = new JTextField();
		txtZIP.setBounds(121, 158, 118, 20);
		panelPrivate.add(txtZIP);
		txtZIP.setColumns(10);
		
		final JTextField 	txtPhone = new JTextField();
		txtPhone.setBounds(121, 183, 118, 20);
		panelPrivate.add(txtPhone);
		txtPhone.setColumns(10);
		
		final JTextField 	txtMail = new JTextField();
		txtMail.setBounds(121, 208, 118, 20);
		panelPrivate.add(txtMail);
		txtMail.setColumns(10);
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setText(nextID);
		lblShowID.setBounds(121, 11, 118, 14);
		panelPrivate.add(lblShowID);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveNewPrivate(nextContactID, txtFName.getText(), txtLName.getText(), txtCPR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText());
			}
		});
		btnSave.setBounds(10, 241, 89, 23);
		panelPrivate.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(179, 241, 89, 23);
		panelPrivate.add(btnCancel);
		
		panelPrivate.setVisible(false);
		
	}
	
	private void drawCompany()
	{
		panelCompany = new JPanel();
		panelCompany.setBackground(new Color(255, 255, 204));
		panelCompany.setBounds(10, 45, 278, 275);
		contentPane.add(panelCompany);
		panelCompany.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 89, 14);
		panelCompany.add(lblId);
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setText(Integer.toString(create.getNextContactId()));
		lblShowID.setBounds(121, 11, 118, 14);
		panelCompany.add(lblShowID);

		JLabel lblFirstName = new JLabel("Name:");
		lblFirstName.setBounds(10, 36, 89, 14);
		panelCompany.add(lblFirstName);
		
		JLabel lblCpr = new JLabel("CVR:");
		lblCpr.setBounds(10, 61, 89, 14);
		panelCompany.add(lblCpr);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 86, 89, 14);
		panelCompany.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(10, 111, 89, 14);
		panelCompany.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(10, 136, 89, 14);
		panelCompany.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 161, 89, 14);
		panelCompany.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 186, 89, 14);
		panelCompany.add(lblEmail);
		
		final JTextField txtName = new JTextField();
		txtName.setBounds(121, 33, 118, 20);
		panelCompany.add(txtName);
		txtName.setColumns(10);

		final JTextField txtCVR = new JTextField();
		txtCVR.setBounds(121, 58, 118, 20);
		panelCompany.add(txtCVR);
		txtCVR.setColumns(10);
		
		final JTextField 	txtAddress = new JTextField();
		txtAddress.setBounds(121, 83, 118, 20);
		panelCompany.add(txtAddress);
		txtAddress.setColumns(10);
		
		final JTextField 	txtCity = new JTextField();
		txtCity.setBounds(121, 108, 118, 20);
		panelCompany.add(txtCity);
		txtCity.setColumns(10);
		
		final JTextField 	txtZIP = new JTextField();
		txtZIP.setBounds(121, 133, 118, 20);
		panelCompany.add(txtZIP);
		txtZIP.setColumns(10);
		
		final JTextField 	txtPhone = new JTextField();
		txtPhone.setBounds(121, 158, 118, 20);
		panelCompany.add(txtPhone);
		txtPhone.setColumns(10);
		
		final JTextField 	txtMail = new JTextField();
		txtMail.setBounds(121, 183, 118, 20);
		panelCompany.add(txtMail);
		txtMail.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveNewBusiness(nextContactID, txtName.getText(), txtCVR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText());
				
			}
		});
		btnSave.setBounds(10, 241, 89, 23);
		panelCompany.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(179, 241, 89, 23);
		panelCompany.add(btnCancel);
		
		panelCompany.setVisible(false);
	}

	private void saveNewPrivate(int id, String fName, String lName, String cpr, String address, String city, String zip, String phone, String eMail)
	{
		if(fName.isEmpty() || lName.isEmpty() || cpr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                    errorZ=false;
                    try
                    {
                            Integer.valueOf(zip);
                    }
                    catch(Exception e)
                    {
                            JOptionPane.showMessageDialog(null,
                                            "The ZIP code should be a number!",
                                            "Warning",
                                            JOptionPane.WARNING_MESSAGE);
                            errorZ=true;
                    }

                    if(errorZ!=true)
                    {       
                            ModelLayer.Private p = new ModelLayer.Private(id, address, city, zip, phone, eMail, "active", cpr, fName, lName);
                            try {
								create.createCustomer(p);
							} catch (Exception e) {
								JOptionPane.showMessageDialog(null,
		                                "Something went wrong, please try again!",
		                                "Warning",
		                                JOptionPane.WARNING_MESSAGE);
								e.printStackTrace();
							}
                            MainWindow.getInstance().refreshTableCustomer();
                            dispose();
                    }
            }
		}
	}
	
	private void saveNewBusiness(int id, String name, String cvr, String address, String city, String zip, String phone, String eMail)
	{
		if(name.isEmpty() || cvr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                    errorZ=false;
                    try
                    {
                            Integer.valueOf(zip);
                    }
                    catch(Exception e)
                    {
                            JOptionPane.showMessageDialog(null,
                                            "The ZIP code should be a number!",
                                            "Warning",
                                            JOptionPane.WARNING_MESSAGE);
                            errorZ=true;
                    }

                    if(errorZ!=true)
                    {       
                            ModelLayer.Business b = new ModelLayer.Business(id, address, city, zip, phone, eMail, "active", cvr, name);
                            try {
								create.createCustomer(b);
							} catch (Exception e) {
								JOptionPane.showMessageDialog(null,
		                                "Something went wrong, please try again!",
		                                "Warning",
		                                JOptionPane.WARNING_MESSAGE);
								e.printStackTrace();
							}
                            MainWindow.getInstance().refreshTableCustomer();
                            dispose();
                    }
            }
		}
	}
}
